<?php
class ChitieudvsController extends AppController {

	var $name = 'Chitieudvs';
    var $helpers=array('Ctable','Js','Jsrt');
    var $uses = array('Chitieudv','Diemchuan','Kehoach','Thuocdv');
    var $components = array('RequestHandler');
	    
    function index() {
		$this->Chitieudv->recursive = 0;
		$this->set('chitieudvs', $this->paginate());
	}
    
	function themchitieu($kehoach_id=null,$themchitieu=1)
	{	
		
		if ($kehoach_id!=null)
		{
			$dalapkh = $this->Kehoach->find('list',array('conditions'=>array('Kehoach.id'=>$kehoach_id)));
			if(count($dalapkh)==0)
			{
				$this->Session->setFlash(__('Chỉ Tiêu Chưa Thể Lập Do Chưa Lập Thời Gian Cho Kế Hoạch', true));
				$this->redirect(array('controller'=>'kehoaches','action'=>'index',$this->Session->read('cur_cha')));
			}
			$kehoaches = $this->Kehoach->find('list',array('conditions'=>array('id'=>$kehoach_id),
														'fields'=>array('dagiao')));
			$dagiao = $kehoaches[$kehoach_id];
			if($themchitieu==1)
			{
			/*
			 * kiem tra da lap ke hoach chua
			 */
				
				/*
				 * Kiem tra kehoach nay da giao chi tieu chua
				 */
				$dalap=$this->Chitieudv->find('list',array(
													'conditions'=>array('Chitieudv.kehoach_id'=>$kehoach_id)));
				if (count($dalap)!=0)
				{
					$this->Session->setFlash(__('Chỉ Tiêu Tháng Này Đã Lập không Thể Lập Lại', true));
					$this->redirect(array('controller'=>'chitieudvs','action'=>'hienchitieu',$kehoach_id));
				}
			}
			
			
			if ($dagiao==1)
			{
				$this->Session->setFlash(__('Chỉ Tiêu Đã Giao Không Thể Thay Đổi', true));
				$this->redirect(array('action'=>'hienchitieu',$kehoach_id));
			}	
			for($i=0;$i<$this->data['Chitieudv']['countcheck'];$i++)
			{
				if ($this->data['Chitieudv']['checkbox'.$i]!=0)
					$hanghoas[$this->data['Chitieudv']['checkbox'.$i]]=$this->data['Chitieudv'][$this->data['Chitieudv']['checkbox'.$i]];
			}
			$donvi_id= $this->Session->read('cur_cha');
			$dvcha = $this->Thuocdv->Donvi->find('list',array('conditions'=>array('id'=>$donvi_id),'fields'=>array('madv')));
			$ldv =substr($dvcha[key($dvcha)],0,2);
			if($ldv!='CN')
				$loaidv='CN';
			else
				$loaidv ='TT';
			
			$dv = $this->Thuocdv->find('list',array(
											'conditions'=>array('donvi_id'=>$donvi_id),
											'fields'=>array('donvi','donvi')));
			$donvis = $this->Chitieudv->Donvi->find('list',array(
										'conditions'=>array('LEFT(Donvi.madv,2)'=>$loaidv,'id'=>$dv),
											'fields'=>array('id','madv')));
			$col1[]= "Điểm Chuẩn";
			$thoigian = $this->Kehoach->find('list',array(
																		'recursive'=>0,'contain'=>array('Thoigian'),
																		'conditions'=>array('Kehoach.id'=>$kehoach_id),
																		'fields'=>array('Thoigian.ngaybd','Thoigian.ngaykt')));
			$this->set(compact('hanghoas','donvis','col1','kehoach_id','thoigian','themchitieu'));
		}
		else
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
	}	
	
	function chonchitieu($kehoach_id=null,$themchitieu=1)
	{
		if ($kehoach_id!=null)
		{
			/*
			 * kiem tra da lap chua
			 */
			$dalapkh = $this->Kehoach->find('list',array('conditions'=>array('Kehoach.id'=>$kehoach_id)));
			if(count($dalapkh)==0)
			{
				$this->Session->setFlash(__('Chỉ Tiêu Chưa Thể Lập Do Chưa Lập Thời Gian Cho Kế Hoạch', true));
				$this->redirect(array('controller'=>'kehoaches','action'=>'index',$this->Session->read('cur_cha')));
			}
			if ($themchitieu==1)
			{
				/*
				 * Kiem tra kehoach nay da giao chi tieu chua
				 */
				$dalap=$this->Chitieudv->find('list',array(
													'conditions'=>array('Chitieudv.kehoach_id'=>$kehoach_id)));
				if (count($dalap)!=0)
				{
					$this->Session->setFlash(__('Chỉ Tiêu Tháng Này Đã Lập không Thể Lập Lại', true));
					$this->redirect(array('controller'=>'chitieudvs','action'=>'hienchitieu',$kehoach_id));
				}
				/*
				 * Kiem tra kehoach nay da giao chi tieu chua
				 */
				$dagiao=$this->Chitieudv->find('list',array(
														'recursive'=>0,
														'conditions'=>array('kehoach_id'=>$kehoach_id),
														'fields'=>array('Kehoach.dagiao')));				
				if (!empty($dagiao))
				{
					if ($dagiao[key($dagiao)]!=0)
					{
						$this->Session->setFlash(__('Chỉ Tiêu Tháng Này Đã Giao không Thể Lập Lại', true));
						$this->redirect(array('controller'=>'chitieudvs','action'=>'hienchitieu',$kehoach_id));
					}
				}
				
				$hanghoas = $this->Chitieudv->Hanghoa->find('list');
				$this->set(compact('hanghoas','kehoach_id','themchitieu'));
			}
			else
			{
				$hh_dalap = $this->Chitieudv->find('list',array(
															'conditions'=>array('kehoach_id'=>$kehoach_id),
															'fields'=>array('hanghoa_id'),
															'group'=>array('hanghoa_id')));
				
				$hanghoas = $this->Chitieudv->Hanghoa->find('list',array(
															'conditions'=>array('NOT'=>array('id'=>$hh_dalap))));
				$this->set(compact('hanghoas','kehoach_id','themchitieu'));
			}
		}
		else
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
	}
	
	function suachitieu($kehoach_id=null)
	{
		if ($kehoach_id!=null)
		{
			$kehoaches = $this->Kehoach->find('list',array('conditions'=>array('id'=>$kehoach_id),
														'fields'=>array('dagiao')));
			$dagiao = $kehoaches[$kehoach_id];
			if ($dagiao == 1)
			{
				$this->Session->setFlash(__('Chỉ Tiêu Đã Giao Không Được Thay Đổi', true));
				$this->redirect(array('action'=>'hienchitieu',$kehoach_id));
			}
			/*
			 * Kiem tra kehoach nay da giao chi tieu chua
			 */
			$dalap=$this->Chitieudv->find('list',array(
												'conditions'=>array('Chitieudv.kehoach_id'=>$kehoach_id)));
			if (count($dalap)==0)
			{
				$this->Session->setFlash(__('Chỉ Tiêu Chưa Đươc lập cho kế hoạch Này', true));
				$this->redirect(array('controller'=>'Kehoaches','action'=>'index',$kehoach_id));
			}
			
			/*
			 * lay data cho diem chuan
			 */
			$diems =$this->Diemchuan->find('list',array(
									'conditions'=>array('kehoach_id'=>$kehoach_id),
									'fields'=>array('hanghoa_id','diem')));
			$diemchuanid =$this->Diemchuan->find('list',array(
									'conditions'=>array('kehoach_id'=>$kehoach_id),
									'fields'=>array('hanghoa_id','id')));
			$col1[]="Điểm Chuẩn";
			/*
			 * lay data cua chitieu
			 */
			$thoigian = $this->Kehoach->find('list',array(
																		'recursive'=>0,'contain'=>array('Thoigian'),
																		'conditions'=>array('Kehoach.id'=>$kehoach_id),
																		'fields'=>array('Thoigian.ngaybd','Thoigian.ngaykt')));
			$hanghoa = $this->Chitieudv->find('list',array(
											'recursive'=>0,'contain'=>array('Hanghoa'),
											'conditions'=>array('Chitieudv.kehoach_id'=>$kehoach_id),
											'fields'=>array('Chitieudv.hanghoa_id','Hanghoa.tenhh')));
			$donvi = $this->Chitieudv->find('list',array(
											'recursive'=>0,'contain'=>array('Donvi'),
											'conditions'=>array('Chitieudv.kehoach_id'=>$kehoach_id),
											'fields'=>array('Chitieudv.donvi_id','Donvi.madv')));
			$chitieudvs = $this->Chitieudv->find('list',array(
												'conditions'=>array('Chitieudv.kehoach_id'=>$kehoach_id),
												'fields'=>array('Chitieudv.hanghoa_id','Chitieudv.soluong','Chitieudv.donvi_id')));
			
			$chitieudvs_id = $this->Chitieudv->find('list',array(
												'conditions'=>array('kehoach_id'=>$kehoach_id),
												'fields'=>array('Chitieudv.hanghoa_id','Chitieudv.id','Chitieudv.donvi_id')));
			if(!empty($chitieudvs_id))
			{
			$dong = 0;
			$cot = 0;
			foreach ($donvi as $colkey => $colvalue):
				$dong=0;
				foreach ($hanghoa as $rowkey=>$rowvalue):
					$data2[$dong][$cot]= $chitieudvs[$colkey][$rowkey];
					$data_id2[$dong][$cot]=$chitieudvs_id[$colkey][$rowkey];
					$data1[$dong][0]=$diems[$rowkey];
					$data_id1[$dong][0]= $diemchuanid[$rowkey];
					$dong++;
				endforeach;
				$cot++;
			endforeach;
			}
			$this->set(compact('col1','donvi','hanghoa','data1','data_id1','data2','data_id2','kehoach_id','thoigian'));
		}
	else
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
	}
    
	function luuchitieu($kehoach_id=null,$themchitieu=1)
	{
		if ($kehoach_id!=null)
		{
			/*
			 * lay data
			 */
			$colnum2= $this->data['Chitieudv']['colnum2'];
			$rownum = $this->data['Chitieudv']['rownum'];
			$thoigian = $this->Kehoach->find('list',array(
																	'recursive'=>0,'contain'=>array('Thoigian'),
																	'conditions'=>array('Kehoach.id'=>$kehoach_id),
																	'fields'=>array('Thoigian.ngaybd','Thoigian.ngaykt','Thoigian.id')));
			$thoigian_id=key($thoigian);
			
			/*
			 *  Luu Diem chuan
			 */
			 for($j=0;$j<$rownum;$j++)
		     {
				if(isset($this->data['Chitieudv']['col_1']['id'][$j][0]))
					$id_dc=$this->data['Chitieudv']['col_1']['id'][$j][0]; 
		        $kehoach_dc=$kehoach_id;
		        $hanghoa_dc=$this->data['Chitieudv']['row'][$j];
		        if ($this->data['Chitieudv']['col_1'][$j][0]!=NULL)
		        	$diemchuan=$this->data['Chitieudv']['col_1'][$j][0];
		        else
		            $diemchuan=0;
		        if(isset($id_dc))
		         	$qstr= "update diemchuans set diem=".$diemchuan." where id=".$id_dc;
		        else
		           	$qstr= "insert into diemchuans(hanghoa_id,kehoach_id,diem) values(".$hanghoa_dc.",".$kehoach_dc.",".$diemchuan.")";
		        $this->Chitieudv->query($qstr);
		    }
		    /*
		     * Luu chi tieu don vi
		     */
		    $ins =1;
			for($i=0;$i<$colnum2;$i++)
			{
				for($j=0;$j<$rownum;$j++)
				{
					if(isset($this->data['Chitieudv']['col_2']['id'][$j][$i]))
					{
						$record['Chitieudv']['id']=$this->data['Chitieudv']['col_2']['id'][$j][$i];
						$ins=0;
					} 
		             $record['Chitieudv']['kehoach_id']=$kehoach_id;
		             $record['Chitieudv']['donvi_id']=$this->data['Chitieudv']['col2'][$i];
		             $record['Chitieudv']['hanghoa_id']=$this->data['Chitieudv']['row'][$j];
		             if ($this->data['Chitieudv']['col_2'][$j][$i]!=NULL)
		                $record['Chitieudv']['soluong']=$this->data['Chitieudv']['col_2'][$j][$i];
		             else
		                $record['Chitieudv']['soluong']=0;
		             if(!$this->Chitieudv->saveAll($record))
		             	$this->Session->setFlash(__('Lưu Thất Bại', true));
		    	}
		    	/*if ($ins==1 && $themchitieu==1)
		        {
		             $dv_id=$this->data['Chitieudv']['col2'][$i];
		             $ngaybd=key($thoigian[key($thoigian)]);
		             $ngaykt=$thoigian[key($thoigian)][key($thoigian[key($thoigian)])];
		             $strins = "insert into kehoaches(thoigian_id,donvi_id,ngaybd,ngaykt) values (".$thoigian_id.",".$dv_id.",'".$ngaybd."','".$ngaykt."')";
		             $this->Kehoach->query($strins);
		        } */
			}
	    	$this->Session->setFlash(__('Đã Lập Chỉ Tiêu', true));
			$this->redirect(array('action' => 'hienchitieu',$kehoach_id));
		}
		else
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
	}
	
	function hienchitieu($kehoach_id=null)
	{
		if ($kehoach_id!=null)
		{
			$thoigian = $this->Chitieudv->Kehoach->find('list',array(
																	'recursive'=>0,'contain'=>array('Thoigian'),
																	'conditions'=>array('Kehoach.id'=>$kehoach_id),
																	'fields'=>array('Thoigian.ngaybd','Thoigian.ngaykt','Thoigian.id')));
			if ($this->RequestHandler->isAjax())
			{
				$dvs = $this->Chitieudv->find('list',array(
													'conditions'=>array('kehoach_id'=>$kehoach_id),
													'fields'=>array('donvi_id'),
													'group'=>array('donvi_id')));
				$thoigian_id = key($thoigian);
				$this->Chitieudv->Kehoach->id=$kehoach_id;
				$this->Chitieudv->Kehoach->saveField('dagiao',1);
				
			}		
			/*
			 * lay data ve diem chuan
			 */
			$col1[]="Điểm Chuẩn"; 
			$diems =$this->Diemchuan->find('list',array(
									'recursive'=>0,'contain'=>array('Hanghoa'),
									'conditions'=>array('kehoach_id'=>$kehoach_id),
									'fields'=>array('Hanghoa.tenhh','diem')));
			/*
			 *  lay data so luong don vi
			 */	
			$chitieudvs = $this->Chitieudv->find('list',array('recursive'=>0,'contain'=>array('Hanghoa','Donvi'),
												'conditions'=>array('kehoach_id'=>$kehoach_id),
												'fields'=>array('Hanghoa.tenhh','Chitieudv.soluong','Donvi.madv')));
			$kehoaches = $this->Chitieudv->Kehoach->find('list',array('conditions'=>array('id'=>$kehoach_id),
														'fields'=>array('dagiao')));
			/*
			 * lay hanghoa_id va kehoach_id
			 */
			/*$ds = $this->Diemchuan->find('list',array(
												'conditions'=>array('kehoach_id'=>$kehoach_id),
												'fields'=>array('kehoach_id','hanghoa_id')));*/
			$dagiao = $kehoaches[$kehoach_id];
			if (count($chitieudvs)!=0)
			{
				$col_ctdv =array_keys($chitieudvs);
				$col_ctdv[]="Tổng";
				$row_ctdv =Null;
				
				if (count($col_ctdv)!=0)
					$row_ctdv = array_keys($chitieudvs[$col_ctdv[0]]);
					
				$j=0;
				foreach ($row_ctdv as $key => $value)
				{
					$tong=0;
					for ($i=0;$i<count($col_ctdv)-1;$i++)
					{
						$data2[$j][$i]= $chitieudvs[$col_ctdv[$i]][$row_ctdv[$j]];
						$tong +=$data2[$j][$i];		
					}
					$data2[$j][$i]=$tong;
					$data1[$j][0] = $diems[$value];
					$j++;
				}
				
			}
			$hanghoas = $this->Chitieudv->find('list',array('recursive'=>0,'conditions'=>array('kehoach_id'=>$kehoach_id),
																					'fields'=>array('Hanghoa.id','Hanghoa.tenhh')));
			
			$this->set(compact('col_ctdv','col1','row_ctdv','data1','data2','thoigian','kehoach_id','dagiao','hanghoas'));
		}
		else
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
	}

	function xoahang($kehoach_id=0,$hanghoa_id=0)
	{
		if ($kehoach_id==0)
		{
			$this->redirect(array('action'=>'hienchitieu',$kehoach_id));
		}
		else
		{
			$kehoaches = $this->Chitieudv->Kehoach->find('list',array('conditions'=>array('id'=>$kehoach_id),
													'fields'=>array('dagiao')));
			$dagiao = $kehoaches[$kehoach_id];
			if ($dagiao==1)
			{
				$this->Session->setFlash(__('Chỉ Tiêu Đã Giao Không Thể Xóa', true));
				$this->redirect(array('action'=>'hienchitieu',$kehoach_id));
			}
			if ($hanghoa_id!=0)
			{
				$this->Chitieudv->deleteAll(array('kehoach_id'=>$kehoach_id,'hanghoa_id'=>$hanghoa_id),FALSE);
				$this->Diemchuan->deleteAll(array('kehoach_id'=>$kehoach_id,'hanghoa_id'=>$hanghoa_id),FALSE);
			}
			else
			{
				$this->Chitieudv->deleteAll(array('kehoach_id'=>$kehoach_id),FALSE);
				$this->Diemchuan->deleteAll(array('kehoach_id'=>$kehoach_id),FALSE);
			}
			$this->redirect(array('action'=>'hienchitieu',$kehoach_id));
		}
	}

	function hienctduocgiao($kehoach_id=null)
	{
		if ($kehoach_id==null)
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		else 
		{
		$thoigian = $this->Chitieudv->Kehoach->find('list',array(
																'recursive'=>0,'contain'=>array('Thoigian'),
																'conditions'=>array('Kehoach.id'=>$kehoach_id),
																'fields'=>array('Thoigian.ngaybd','Thoigian.ngaykt','Thoigian.id')));
		/*
		 * lay data ve diem chuan
		 */
		$col1[]="Điểm Chuẩn"; 
		$diems =$this->Diemchuan->find('list',array(
								'recursive'=>0,'contain'=>array('Hanghoa'),
								'conditions'=>array('kehoach_id'=>$kehoach_id),
								'fields'=>array('Hanghoa.tenhh','diem')));
		/*
		 *  lay data so luong don vi
		 */	
		$chitieudvs = $this->Chitieudv->find('list',array('recursive'=>0,'contain'=>array('Hanghoa','Donvi'),
											'conditions'=>array('kehoach_id'=>$kehoach_id),
											'fields'=>array('Hanghoa.tenhh','Chitieudv.soluong','Donvi.madv')));
		$kehoaches = $this->Chitieudv->Kehoach->find('list',array('conditions'=>array('id'=>$kehoach_id),
													'fields'=>array('dagiao')));
		/*
		 * lay hanghoa_id va kehoach_id
		 */
		/*$ds = $this->Diemchuan->find('list',array(
											'conditions'=>array('kehoach_id'=>$kehoach_id),
											'fields'=>array('kehoach_id','hanghoa_id')));*/
		$dagiao = $kehoaches[$kehoach_id];
		if (count($chitieudvs)!=0)
		{
			$col_ctdv =array_keys($chitieudvs);
			$col_ctdv[]="Tổng";
			$row_ctdv =Null;
			
			if (count($col_ctdv)!=0)
				$row_ctdv = array_keys($chitieudvs[$col_ctdv[0]]);
				
			$j=0;
			foreach ($row_ctdv as $key => $value)
			{
				$tong=0;
				for ($i=0;$i<count($col_ctdv)-1;$i++)
				{
					$data2[$j][$i]= $chitieudvs[$col_ctdv[$i]][$row_ctdv[$j]];
					$tong +=$data2[$j][$i];		
				}
				$data2[$j][$i]=$tong;
				$data1[$j][0] = $diems[$value];
				$j++;
			}
			
		}
		$this->set(compact('col_ctdv','col1','row_ctdv','data1','data2','thoigian','kehoach_id','dagiao'));
		}
	}
	
	function hienchitieuthangcn($kehoach_id=null)
	{
	if ($kehoach_id!=null)
		{
			$thoigian = $this->Chitieudv->Kehoach->find('list',array(
																	'recursive'=>0,'contain'=>array('Thoigian'),
																	'conditions'=>array('Kehoach.id'=>$kehoach_id),
																	'fields'=>array('Thoigian.ngaybd','Thoigian.ngaykt','Thoigian.id')));
			if ($this->RequestHandler->isAjax())
			{
				$dvs = $this->Chitieudv->find('list',array(
													'conditions'=>array('kehoach_id'=>$kehoach_id),
													'fields'=>array('donvi_id'),
													'group'=>array('donvi_id')));
				$thoigian_id = key($thoigian);
				$this->Chitieudv->Kehoach->id=$kehoach_id;
				$this->Chitieudv->Kehoach->saveField('dagiao',1);
				$dagiao=1;
			}		
			/*$thoigian = $this->Chitieudv->Kehoach->find('list',array(
																		'recursive'=>0,'contain'=>array('Thoigian'),
																		'conditions'=>array('Kehoach.id'=>$kehoach_id),
																		'fields'=>array('Thoigian.ngaybd','Thoigian.ngaykt')));*/
			/*
			 * lay data ve diem chuan
			 */
			$col1[]="Điểm Chuẩn"; 
			$diems =$this->Diemchuan->find('list',array(
									'recursive'=>0,'contain'=>array('Hanghoa'),
									'conditions'=>array('kehoach_id'=>$kehoach_id),
									'fields'=>array('Hanghoa.tenhh','diem')));
			/*
			 *  lay data so luong don vi
			 */	
			$chitieudvs = $this->Chitieudv->find('list',array('recursive'=>0,'contain'=>array('Hanghoa','Donvi'),
												'conditions'=>array('kehoach_id'=>$kehoach_id),
												'fields'=>array('Hanghoa.tenhh','Chitieudv.soluong','Donvi.madv')));
			$kehoaches = $this->Chitieudv->Kehoach->find('list',array('conditions'=>array('id'=>$kehoach_id),
														'fields'=>array('dagiao')));
			/*
			 * lay hanghoa_id va kehoach_id
			 */
			/*$ds = $this->Diemchuan->find('list',array(
												'conditions'=>array('kehoach_id'=>$kehoach_id),
												'fields'=>array('kehoach_id','hanghoa_id')));*/
			$dagiao = $kehoaches[$kehoach_id];
			if (count($chitieudvs)!=0)
			{
				$col_ctdv =array_keys($chitieudvs);
				$col_ctdv[]="Tổng";
				$row_ctdv =Null;
				
				if (count($col_ctdv)!=0)
					$row_ctdv = array_keys($chitieudvs[$col_ctdv[0]]);
					
				$j=0;
				foreach ($row_ctdv as $key => $value)
				{
					$tong=0;
					for ($i=0;$i<count($col_ctdv)-1;$i++)
					{
						$data2[$j][$i]= $chitieudvs[$col_ctdv[$i]][$row_ctdv[$j]];
						$tong +=$data2[$j][$i];		
					}
					$data2[$j][$i]=$tong;
					$data1[$j][0] = $diems[$value];
					$j++;
				}
				
			}
			$this->set(compact('col_ctdv','col1','row_ctdv','data1','data2','thoigian','kehoach_id','dagiao'));
		}
		else
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
	}

	function giaoct($kehoach_id=null)
	{
			$this->layout='ajax';
			$dagiao=0;
			$kh = $this->Kehoach->find('first',array('conditions'=>array('Kehoach.id'=>$kehoach_id)));
			$dv_id = $kh['Kehoach']['donvi_id'];
			$ngaybd= $kh['Kehoach']['ngaybd'];
			$ngaykt = $kh['Kehoach']['ngaykt'];
			
			$khcha= $this->Chitieudv->find('list',array(
											'recursive'=>0,
				    						'conditions'=>array('Chitieudv.donvi_id'=>$dv_id,
				    											'ngaybd <='=>$ngaybd,
				    											'ngaykt >='=>$ngaykt),
											'fields'=>array('Kehoach.dagiao')));
			$dv_cha =$this->Session->read('cur_cha');
			$dv = $this->Chitieudv->Donvi->find('first',array('recursive'=>-1,'conditions'=>array('id'=>$dv_cha)));
			$dagiao=0;
			if(substr($dv['Donvi']['madv'],0,2)!='CN')
			{
				$this->Kehoach->id=$kehoach_id;
				$this->Kehoach->saveField('dagiao',1);
				$dagiao=1;
			}
			if(!empty($khcha))
			{
				
				if($khcha[key($khcha)]==1)
				{
					$this->Kehoach->id=$kehoach_id;
					$this->Kehoach->saveField('dagiao',1);
					$dagiao=1;
				}
			}

			$this->set(compact('dagiao','kehoach_id'));
	}

	function xoachitieu($kehoach_id=null)
	{
		if(!$kehoach_id)
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		
		$kehoach = $this->Kehoach->find('first',array('recursive'=>-1,'conditions'=>array('Kehoach.id'=>$kehoach_id)));
		$kqbh = $this->Chitieudv->Kqbanhangtt->find('list',array('recursive'=>0,'conditions'=>array('Chitieudv.kehoach_id'=>$kehoach_id)));
		
		if($kehoach['Kehoach']['dagiao']==0 && empty($kqbh))
		{
			$hanghoas = $this->data['Chitieudv'];
			foreach ($hanghoas as $key => $value)
			{
				if($key == $value)
				{
					$qstr= "delete from diemchuans where hanghoa_id= ".$key." and kehoach_id= ".$kehoach_id;
					$this->Chitieudv->query($qstr);
					$this->Chitieudv->deleteAll(array('hanghoa_id'=>$key,'kehoach_id'=>$kehoach_id));
				}
			}
			$this->Session->setFlash(__('Đã xóa chỉ Tiêu ', true));
		}
		else
		{
			$this->Session->setFlash(__('Chỉ Tiêu Đã Giao hoặc đã báo cáo không Thể Xóa', true));
		}
		$this->redirect(array('action'=>'hienchitieu',$kehoach_id));
	}

}
?>